/**
 * form.builder.js 表单设计器相关API
 */
import request from '../utils/request'
import Qs from 'qs'

/**
 * 保存表单定义信息
 * @param data
 * @returns
 */
export const saveFormDefinition = (data) => {
    return request({
        url: "/app/rest/models",
        method: 'post',
        data: data
    })
}

/**
 * 保存表单定义的JSON字符串
 * @param data
 * @param modelId
 * @returns {AxiosPromise}
 */
export const saveFormJsonData = (data,modelId) => {
    return request({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: `/app/rest/models/${modelId}/editor/json`,
        method: 'post',
        data: Qs.stringify(data)
    });
}

export const getFormJsonData = (modelId) =>{
    return request({
        url: `/app/rest/models/${modelId}/editor/json`,
        method: 'get'
    });
}

/**
 * 获取表单定义信息
 * @param query
 * @returns
 */
export const getFormDefinition = (query) => {
    return request({
        url: "/app/rest/models",
        method: 'get',
        params: query
    })
}

export const getFormDefinitionById = (id) => {
    return request({
        url: "/app/rest/models/" + id,
        method: 'get'
    })
}

/**
 * 更新表单定义信息
 * @param data
 * @returns
 */
export const updateFormModel = (data) => {
    return request.put('/app/rest/models/' + data.id,data);
}

/**
 * 删除表单定义
 * @param modelId
 * @returns
 */
export const deleteFormModel = (modelId) => {
    return request({
        url: '/app/rest/models/' + modelId,
        method: 'delete'
    });
}
